package dao.xml;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;

import model.Adresse;
import model.Anlage;
import model.Teilnehmer;
import model.Termin;
import model.Terminplaner;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class TerminXstream implements DefaultDAO<Terminplaner> {

	@Override
	public Terminplaner read(InputStream is) throws IOException {
		if (is == null) 
			throw new IllegalArgumentException("InputStream darf nicht null sein!");
				
	    return (Terminplaner) getXStream().fromXML(is);
	
		
	}

	@Override
	public Collection<Termin> readMultiple(InputStream is) {

		throw new UnsupportedOperationException("Lesen von mehreren Terminplanern nicht unterstuetzt!");
		
	}

	@Override
	public void write(OutputStream os, Terminplaner data) throws IOException {
		if (os == null) {
			throw new IllegalArgumentException("OutputStream darf nicht null sein!");
		}
		if (data == null) {
			throw new IllegalArgumentException("data darf nicht null sein!");
		}
		
		getXStream().toXML(data, os);
		os.write(10);
		
		os.flush();
	}

	@Override
	public void writeMultiple(OutputStream os, Collection<Terminplaner> data)
 {
		throw new UnsupportedOperationException("Schreiben von mehreren Terminplanern nicht unterstuetzt!");

	}

	private XStream getXStream() {
		XStream xstream = new XStream(new DomDriver("UTF-8"));

		

		xstream.registerConverter(new DateConverter());
		xstream.registerConverter(new TerminArtConverter());
		xstream.registerConverter(new TreeSetConverter(xstream));
		
		xstream.addImplicitCollection(Terminplaner.class, "termine");
		xstream.alias("terminplaner", Terminplaner.class);
		
		xstream.alias("termin", Termin.class);
		xstream.useAttributeFor(Termin.class, "thema");
		xstream.useAttributeFor(Termin.class, "beschreibung");
		xstream.useAttributeFor(Termin.class, "terminAnfang");
		xstream.aliasField("terminanfang", Termin.class, "terminAnfang");

		xstream.useAttributeFor(Termin.class, "terminEnde");
		xstream.aliasField("terminende", Termin.class, "terminEnde");

		xstream.useAttributeFor(Termin.class, "terminArt");
		xstream.aliasField("terminart", Termin.class, "terminArt");

		xstream.useAttributeFor(Termin.class, "prioritaet");
		// ---adresse
		xstream.alias("adresse", Adresse.class);
		xstream.useAttributeFor(Adresse.class, "ort");
		xstream.useAttributeFor(Adresse.class, "plz");
		xstream.useAttributeFor(Adresse.class, "strasse");
		xstream.useAttributeFor(Adresse.class, "land");
		// ---teilnehmer

		
		xstream.alias("teilnehmdfgsdfgfd", Set.class, TreeSet.class);

		
		xstream.alias("teilnehmer", Teilnehmer.class);


		xstream.useAttributeFor(Teilnehmer.class, "vorname");
		xstream.useAttributeFor(Teilnehmer.class, "nachname");
		xstream.useAttributeFor(Teilnehmer.class, "anrede");
		
		//--- anlagen

		xstream.alias("anlage", Anlage.class);

		xstream.useAttributeFor(Anlage.class, "beschreibung");
		xstream.useAttributeFor(Anlage.class, "pfad");
		xstream.useAttributeFor(Anlage.class, "timestamp");

		return xstream;
	}

}